package test.enba.mysql.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import test.enba.mysql.entity.User;
import test.enba.mysql.service.UserService;

@RestController
@RequestMapping("/mysql/user")
public class UserController {

  @Autowired private UserService userService;

  @GetMapping("/list")
  public List<User> list() {
    return userService.list();
  }

  @PostMapping("/save")
  public boolean save(@RequestBody User user) {
    return userService.save(user);
  }

  @GetMapping("/{id}")
  public User getById(@PathVariable Long id) {
    return userService.getById(id);
  }

  @PutMapping("/update")
  public boolean update(@RequestBody User user) {
    return userService.updateById(user);
  }

  @DeleteMapping("/{id}")
  public boolean delete(@PathVariable Long id) {
    return userService.removeById(id);
  }

  // 模拟从主库获取数据
  @GetMapping("/master/list")
  public List<User> masterList() {
    return userService.getMasterUsers();
  }

  // 模拟从从库获取数据
  @GetMapping("/slave/list")
  public List<User> slaveList() {
    return userService.getSlaveUsers();
  }
}
